
<script type="text/javascript">
    //树表格

    $(function () {
        $('#tt').treegrid({
            url          : '<?=site_url("admin/authrule/index");?>?d=d',
            idField      : 'id',
            title        : '后台菜单列表',
            fitColumns   : true,
            treeField    : 'text',
            collapseAll  : true,
            fit          : true,
            doSize       : true,
            closable     : true,
            striped      : true,
            rownumbers   : true,
            toolbar      : $("#j_auto_order_form"),
            onDblClickRow:function(){showDialog("修改");},
            checkbox     : true,
            columns      : [[
                {field: 'ck', width: 150, checkbox: true},
                {field: 'text', title: '标题', width: 150, state: 'closed'},
                {field: 'id', title: 'ID'},
                {field: 'url', title: 'URL'},
                {field: 'module', title: '模块'},
                {field: 'action', title: '控制器'},
                {field: 'method', title: '方法'},
                {field: 'condition', title: '条件', width: 80},
                {field: 'pid', title: '父ID'},
                {
                    field    : 'status',
                    title    : '状态',
                    width    : 80,
                    formatter: function (value, row, index) {
                        if (value == 1)return "<span style='color:darkgreen;'>可用</span>";
                        else return "<span style='color:red;'>禁用</span>";
                    }
                },
                {
                    field    : 'type',
                    title    : '类型',
                    width    : 80,
                    formatter: function (value, row, index) {
                        if (value == 1)return "<strong>菜单组</strong>";
                        else if (value == 2) return "页面";
                        else return "<i>操作</i>";
                    }
                },
                {
                    field : 'listorder',
                    title : '排序',
                    width : 80,
                    editor: {
                        type   : 'text',
                        options: {}
                    }
                },
                {
                    field: 'eieieei',
                    title: '操作',
                    width: 150,
                    resizable:true,
                    align:'center',
                    formatter:function(value,row,index)
                    {
                        return "<span onclick='edit("+row.id+")'>编辑</span> | <span onclick='del("+row.id+")'>删除</span>";
                    }
                }
            ]],
            onLoadSuccess:function(data){
                $(".edit-button").linkbutton({
                    iconCls: 'icon-edit'
                });
                $(".delete-button").linkbutton({
                    iconCls: 'icon-remove'
                });
            }
        });

    })
    //方法
    function showDialog(option) {

        $("#form").dialog({
            title  : option + '数据',
            closed : false,
            cache  : false,
            modal  : true,
            buttons: [{
                text   : '保存',
                iconCls:'icon-ok',
                handler: function () {
                    saveData();
                }
            }, {
                text   : '关闭',
                iconCls:'',
                handler: function () {
                    $("#form").dialog('close');
                }
            }]
        });

        $("#form").form("clear");
        $("#form #pid").combotree({
            url:'<?= site_url( "admin/authrule/index" ); ?>?d=select'
        });
        if (option == "修改") {
            var node = $('#tt').treegrid('getSelected');
            $('#form').form('load', node);
        }
        else {
            $("#pid").combotree("setValue", 0);
            $("#status").combobox('setValue',1);
            $("#type").combobox('setValue',1);
        }

    }

    //保存
    function saveData() {
        $("#form").form('submit', {
            url     : "<?=site_url('admin/authrule/save');?>",
            onSubmit: function () {
                var isValid = $(this).form('validate');
                if (!isValid) {
                    $.messager.show({msg: '验证不成功'});	// 如果表单是无效的则隐藏进度条
                }
                return isValid;
            },
            success : function (data) {
                if (data) {
                    $("#tt").treegrid("reload");
                    $.messager.show({msg: '操作成功'});
                } else {
                    $.messager.show({msg: data.error});
                }
                $("#form").dialog("close");
            }
        })
    }

    function del(obj) {
        $("#tt").treegrid('selectRow',obj);
        var node = $('#tt').treegrid('getSelected');
        alert($('#tt').treegrid('getLevel',node.id))
        $.messager.confirm("删除[" + node.text + "]节点", "你确定删除[" + node.text + "]节点及其了节点?", function (r) {
            if (!r) return;
            //ajax后台发送删除数据
            var nodes = $("#tt").treegrid('getChildren', node.id);
            var ids = [];
            for (var i in nodes) {
                ids[i] = nodes[i].id;
            }
            ids.unshift(node.id);
            $.post(
                "<?=site_url('admin/navigate/delete');?>",
                {ids: ids.toString()},
                function (data) {
                    alert(data);
                    $("#tt").treegrid('reload');
                },
                'json'
            );
        });
    }

    function edit(obj)
    {
        $("#tt").treegrid('selectRow',obj);
        showDialog("修改");
    }

</script>
<div id="j_auto_order_form" style="padding: 5px;">
    <span id="btn" onclick="showDialog('添加')" class="easyui-linkbutton" data-options="iconCls:'icon-add'">添加</span>
    <span style="margin-right: 10px;" onclick="window.location.reload();" class="easyui-linkbutton" data-options="iconCls:'icon-reload'">刷新本页面</span>
    <!--<form style="padding:5px;">

        时间: <input class="easyui-datebox j_datebox" name="starttime" value="">
        文章: <input class="easyui-validatebox textbox" type="text" name="orderinfo_goodssn" value="">
        类型: <input class="easyui-validatebox textbox" type="text" name="orderinfo_orderinfosn" value="">
        <a id="j_finish_so" href="#" class="easyui-linkbutton" iconCls="icon-search">条件搜索</a>
        <a id="j_finish_clear" href="#" class="easyui-linkbutton" iconCls="icon-search">清空条件</a>
    </form>-->
</div>
<ul id="tt" class="easyui-tree" data-options="method:'get',animate:true,checkbox:true"></ul>


<form id="form" class="easyui-dialog" data-options="closed:true">
    <table>
        <tr>
            <td width="100" align="right">上级菜单：</td>
            <td width="200">
                <input name="id" type="hidden">
                <select id="pid" style="width:200px;" name="pid"
                        data-options="required:true"></select>
            </td>
            <td></td>
        </tr>
        <tr>
            <td align="right">标　　题：</td>
            <td><input class="easyui-textbox" name="text" required="true"/></td>
            <td style="color:red;">要显示的中文</td>
        </tr>
        <tr>
            <td align="right">模　　块：</td>
            <td><input name="module" class="easyui-textbox"/></td>
            <td></td>
        </tr>
        <tr>
            <td align="right">url：</td>
            <td><input style="width: 200px;" name="url" class="easyui-textbox"/></td>
            <td></td>
        </tr>
        <tr>
            <td align="right">状　　态：</td>
            <td>
                <select id="status" name="status" class="easyui-combobox" required="true">
                    <option value="0">禁用</option>
                    <option value='1'>可用</option>
                </select>
            </td>
            <td></td>
        </tr>
        <tr>
            <td align="right">类　　型：</td>
            <td>
                <select id="type" name="type" class="easyui-combobox" required="true">
                    <option value="1">菜单组</option>
                    <option value='2'>url页面</option>
                    <option value='3'>操作</option>
                </select>
            </td>
        </tr>
        <tr>
            <td align="right">条　　件：</td>
            <td><input name="condition" class="easyui-textbox"/></td>
        </tr>
        <tr>
            <td align="right">排　　序：</td>
            <td><input name="listorder" class="easyui-textbox"/></td>
        </tr>
    </table>
    <!-- <a id="btn" href="#" onclick="save()" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">保存</a>-->
    <!-- <a id="btn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'">取消</a>-->
</form>



